package jp.sourceforge.akjrcp.akj_betas;


import com.akjava.lib.standards.process.SimpleProcessPrinter;
import com.akjava.lib.swt.log.ExecuteLog;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jp.sourceforge.akjrcp.audiotools.AudioToolsPerspective;
import jp.sourceforge.akjrcp.dropsomething.DropsomethingPerspective;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegUI;
import jp.sourceforge.akjrcp.flvtools.basic.Perspective;
import jp.sourceforge.akjrcp.flvtools.explorer.ExplorerPerspective;
import jp.sourceforge.akjrcp.flvtools.search.SearchPerspective;
import jp.sourceforge.akjrcp.psp.PSPPerspective;
import jp.sourceforge.akjrcp.tools.ToolsPerspective;
import jp.sourceforge.akjrcp.util.FFMpegShowStatusProcessPrinter;
import jp.sourceforge.akjrcp.util.StatusBarUtils;
import jp.sourceforge.akjrcp.videotools.VideoToolsPerspective;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext;


/**
 * The main plugin class to be used in the desktop.
 */
public class Akj_betasPlugin extends AbstractUIPlugin {
	public static Log log=LogFactory.getLog(Akj_betasPlugin.class);
	public static final String PLUGIN_ID = "jp.sourceforge.akjrcp.akj_betas";
											
	//The shared instance.
	private static  Akj_betasPlugin plugin;
	
	
	private SimpleProcessPrinter processPrinter=new FFMpegShowStatusProcessPrinter();
	private List<ExecuteLog> executeLogs=new ArrayList<ExecuteLog>();
	public void addExecuteLog(ExecuteLog log){
		executeLogs.add(0,log);//at first;
	}
	//private SimpleProcessPrinter processPrinter=new SimpleProcessPrinter();
	/**
	 * The constructor.
	 * 
	 */
	
	public void startLogSession(){
		String session="######## start "+new Date();
		processPrinter.printOutput(session+"\n");
	}
	
	public void logPrintln(String line){
		line=line.replaceAll("\r\n", "\n");//\r make a problem
		line=line.replaceAll("\r", "");
		
		processPrinter.printOutput(line+"\n");
	}

	public Akj_betasPlugin() {
		plugin = this;
//		log.trace(plugin);
//		log.trace(this);
		/*
		String debug="";
		Properties p=System.getProperties();
		Enumeration keys=p.keys();
		while(keys.hasMoreElements()){
			Object key=keys.nextElement();
			debug+=key+" = "+p.getProperty((String) key)+"\r\n";
		}
		FileWriter writer;
		try {
			writer = new FileWriter("c:\\tmp\\betas.txt");
			writer.write(debug);
			writer.close();
		} catch (IOException e) {
		
			e.printStackTrace();
		}*/
		

		 
	}

	/**
	 * This method is called upon plug-in activation
	 */
	

	

	/*
	private String[] toInstalledDirectory(String[] appNames,String others[]){
		List arrayList=new ArrayList();
		for (int i = 0; i < appNames.length; i++) {
			arrayList.add(System.getProperty("user.dir")+"\\"+appNames[i]);//application dir.
			arrayList.add("c:\\Program Files\\"+appNames[i]);
			arrayList.add(System.getProperty("user.home")+"\\My Documents\\"+appNames[i]);
			arrayList.add(System.getProperty("user.home")+"\\�f�X�N�g�b�v\\"+appNames[i]);
			}
		//add other path
		for (int i = 0; i < others.length; i++) {
			arrayList.add(others[i]);
		}
		return (String[])arrayList.toArray(new String[0]);
	}*/
	public void start(BundleContext context) throws Exception {
		super.start(context);
		/*
		
		*/
		
	}

	/**
	 * This method is called when the plug-in is stopped
	 */
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		plugin = null;
	}

	
	private static String status;
	/**
	 * Returns the shared instance.
	 */
	public static Akj_betasPlugin getDefault() {
		
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path.
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return AbstractUIPlugin.imageDescriptorFromPlugin("akj_betas", path);
	}
	

/**
	 private File toPluginDir(AbstractUIPlugin plugin){
		 	return new File(Platform.getInstallLocation().getURL().getPath(),
		 	"plugins/"
		 	+plugin.getBundle().getSymbolicName()
		 	+"_"
		 	+plugin.getBundle().getHeaders().get("Bundle-Version"));
		 	}
*/
	public SimpleProcessPrinter getProcessPrinter() {
		
		if(status==null){
			status="####";
			String javaVersion=System.getProperty("java.runtime.version");
		
			status+=Messages.getString("akjbetas_title")+",Java "+javaVersion+"\n";
			String pure_ffmpeg=FFMpegUI.getPureFFmpeg();
			String pure_ffmpeg_log="";
			
			
			if(pure_ffmpeg==null || !new File(pure_ffmpeg).exists()){
				pure_ffmpeg_log=Messages.getString("no_pure_ffmpeg.exe");
			}else{
				pure_ffmpeg_log="OK";
			}
			status+="pure_ffmpeg = "+pure_ffmpeg_log+",";
			
			String ffmpeg=FFMpegUI.getFFmpeg();
			String ffmpeg_log="";
			if(ffmpeg==null || !new File(ffmpeg).exists()){
				ffmpeg_log=Messages.getString("no_ffmpeg.exe");
			}else{
				
				if(!new File(new File(ffmpeg).getParent(),"pthreadGC2.dll").exists()){
					if(FFMpegUI.isAlertPThread()){
					ffmpeg_log=Messages.getString("no_pthread.dll");
					}else{
					ffmpeg_log="OK";	
					}
				}else{
					ffmpeg_log="OK";
				
				}
				
			}
			//warn info
			if(!ffmpeg_log.equals("OK")){
				StatusBarUtils.showStatusBar(ffmpeg_log);
			}
			status+="ffmpeg = "+ffmpeg_log+",";
			
			status+="\n####\n";
			plugin.processPrinter.printOutput(status);
		}
		
		
		return processPrinter;
	}

	public void setProcessPrinter(SimpleProcessPrinter processPrinter) {
		this.processPrinter = processPrinter;
	}
	
	public static void addPerspectiveShortcut(IPageLayout layout){
		layout.addPerspectiveShortcut(ExplorerPerspective.ID);
		layout.addPerspectiveShortcut(DropsomethingPerspective.ID);
		layout.addPerspectiveShortcut(PSPPerspective.ID);
		layout.addPerspectiveShortcut(VideoToolsPerspective.ID);
		
		layout.addPerspectiveShortcut(SearchPerspective.ID);
		
		layout.addPerspectiveShortcut(Perspective.ID);
		layout.addPerspectiveShortcut(ToolsPerspective.ID);
		layout.addPerspectiveShortcut(AudioToolsPerspective.ID);
	}
	/**
	 * The constructor.
	 * @deprecated
	 */
	public  String getFFmpeg(){
		return FFMpegUI.getFFmpeg();
	}
	/**
	 * The constructor.
	 * @deprecated
	 */
	public  String getPureFFmpeg(){
		return FFMpegUI.getPureFFmpeg();
	}
		
	/**
	 * 
	 * @deprecated
	 */
	public  String getFFmpegOrPureFFMpeg(){
		return FFMpegUI.getFFmpegOrPureFFMpeg();
	}

	public List<ExecuteLog> getExecuteLogs() {
		return executeLogs;
	}
}
